# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from odoo.exceptions import ValidationError, Warning


class Province(models.Model):
    """
    省
    """
    _name = "tyibs.base.province"
    _order = "order_num"

    name = fields.Char(string=u"名称", size=100, required=True, index=True)
    code = fields.Char(string=u"编码", size=10, required=True, index=True)
    city_ids = fields.One2many('tyibs.base.province.city', 'province_id', string=u'城市')
    order_num = fields.Integer(string=u"显示顺序", default=10, required=True)
    is_default = fields.Boolean(string=u"是否默认")
    remark = fields.Text(string="备注")


class City(models.Model):
    """
    城市
    """
    _name = "tyibs.base.province.city"
    _order = "order_num"

    province_id = fields.Many2one("tyibs.base.province", string="省", required=True)
    name = fields.Char(string=u"名称", size=100, required=True, index=True)
    code = fields.Char(string=u"编码", size=10, required=True, index=True)
    area_ids = fields.One2many('tyibs.base.province.city.area', 'city_id', string=u'区/县')
    order_num = fields.Integer(string=u"显示顺序", default=10, required=True)
    is_default = fields.Boolean(string=u"是否默认")
    remark = fields.Text(string="备注")


class Area(models.Model):
    """
    区
    """
    _name = "tyibs.base.province.city.area"
    _order = "order_num"

    name = fields.Char(string=u"名称", size=100, required=True, index=True)
    code = fields.Char(string=u"编码", size=10, required=True, index=True)
    province_id = fields.Many2one("tyibs.base.province", string="省", required=True)
    city_id = fields.Many2one("tyibs.base.province.city", domain="[('province_id','=',province_id)]", string=u"城市", required=True)
    town_ids = fields.One2many("tyibs.base.province.city.area.town", "area_id", string=u"行政区域")
    ln = fields.Float(string=u"地点经度", digits=(32, 6))
    la = fields.Float(string=u"地点纬度", digits=(32, 6))
    color = fields.Char(string=u"区域颜色", size=10)
    order_num = fields.Integer(string=u"显示顺序", default=10, required=True)
    remark = fields.Text(string="备注")


class Town(models.Model):
    """
    街道(行政区域)
    """
    _name = "tyibs.base.province.city.area.town"
    _order = "order_num"

    name = fields.Char(string=u"名称", size=100, required=True, index=True)
    code = fields.Char(string=u"编码", size=10, required=True, index=True)
    province_id = fields.Many2one("tyibs.base.province", string="省", required=True)
    city_id = fields.Many2one("tyibs.base.province.city", string=u"城市", domain="[('province_id','=',province_id)]", required=True)
    area_id = fields.Many2one("tyibs.base.province.city.area", string=u"区/县", domain="[('city_id','=',city_id)]", required=True)
    order_num = fields.Integer(string=u"显示顺序", default=10, required=True)
    remark = fields.Text(string="备注")

    _sql_constraints = [
        ('code_unique', 'unique(code)', u'编号不能重复'),
    ]


class CardCategory(models.Model):
    """
    维保人员证书种类
    """
    _name = "tyibs.base.maintenance.card.category"

    name = fields.Char(string=u"名称", size=100, required=True, index=True)
    remark = fields.Text(string="备注")

    _sql_constraints = [
        ('name_unique', 'unique(name)', u'名称不能重复'),
    ]


class FaultCategory(models.Model):
    """
    故障类别
    """
    _name = "tyibs.base.fault.category"
    name = fields.Char(string=u"名称", size=100, required=True)
    code = fields.Char(string=u"故障类别代码")
    item_ids = fields.One2many("tyibs.base.fault.item", "category_id", string=u"故障明细")

    _sql_constraints = [
        ('unique_name','unique(name)',u'故障类别的名称不可重复'),
        ('unique_code','unique(code)',u'故障类别代码不可重复')
    ]


class FaultItem(models.Model):
    """
    故障明细
    """
    _name = "tyibs.base.fault.item"
    category_id = fields.Many2one("tyibs.base.fault.category", string=u"故障类别", required=True)
    code = fields.Char(string=u"故障代码")
    name = fields.Char(string=u"内容", size=300, required=True)
    remark = fields.Text(string=u"备注")
    
    _sql_constraints = [
        ('unique_code','unique(code)',u'故障明细代码不可重复')
    ]